<?php



namespace crmeb\listens;


use app\common\model\store\order\StoreOrderStatus;
use app\common\model\system\merchant\Merchant;
use app\common\model\system\sms\SmsRecord;
use app\common\repositories\store\order\StoreOrderRepository;
use app\common\repositories\store\order\StoreOrderStatusRepository;
use app\common\repositories\system\merchant\MerchantRepository;
use crmeb\interfaces\ListenerInterface;
use crmeb\jobs\OrderReplyJob;
use crmeb\jobs\SendSmsJob;
use crmeb\services\TimerService;
use think\facade\Log;
use think\facade\Queue;

class StoreExperienceDueListen extends TimerService implements ListenerInterface
{

    public function handle($event): void
    {
        $this->tick(1000 * 60 * 60 * 2, function () {
            $timer =  2;
            $day = date('Y-m-d');
            $list  = Merchant::getDB()
                ->whereBetween('experience_time', [date('Y-m-d 00:00:00'),date('Y-m-d 23:59:59', strtotime("+ $timer day"))])
                ->where('mer_state', 1)
                ->where('status', 1)
                ->column('mer_id,mer_name,mer_phone,experience_time');
            foreach ($list as $v) {
                try {
                   $info =SmsRecord::getDB()->where('resultcode', null)
                        ->where("DATE_FORMAT(FROM_UNIXTIME('create_time'),'%Y-%m-%d')", '=', $day)
                        ->where('template','1019466048')
                        ->where('phone',$v['mer_phone'])
                        ->find();
                   if(!$info){
                       $smsData = [
                           'supplier' => $v['mer_name'],
                           'daytime'  => $v['experience_time'],
                           'mer_phone'=> $v['mer_phone'],
                       ];
                       Queue::push(SendSmsJob::class, ['tempId' => 'EXPERIENNCE_DUE', 'id' => $smsData]);
                   }
                } catch (\Exception $e) {
                    Log::error('商户体验期到期通知:' . $e->getMessage());
                }
            }
        });
    }
}
